package cn.iocoder.yudao.module.student.controller.app.studentProject;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.api.praxisProject.PraxisProjectApi;
import cn.iocoder.yudao.module.member.api.praxisProject.dto.*;
import cn.iocoder.yudao.module.student.controller.app.studentProject.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -学生端- 项目")
@RestController
@RequestMapping("/student/praxis-project")
@Validated
public class StudentPraxisProjectController {

    @Resource
    private PraxisProjectApi praxisProjectApi;

    @GetMapping("/getPraxisProjectTypeList")
    @ApiOperation("(学生端)获得项目分类")
    @TenantIgnore
    public CommonResult< List<StudentProjectTypeVO>> getPraxisProjectTypeList() {
        List<PraxisProjectTypeDTO> list = praxisProjectApi.getPraxisProjectTypeList();
        List<StudentProjectTypeVO> eList=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            list.stream().forEach(m->{
                StudentProjectTypeVO p=new StudentProjectTypeVO();
                BeanUtils.copyProperties(m,p);
                eList.add(p);
            });
        }
        return success(eList);
    }

    @PostMapping("/page")
    @ApiOperation("(学生端)获得项目分页")
    @TenantIgnore
    public CommonResult<PageResult<StudentProjectVO>> getPraxisProjectPage(@Valid @RequestBody StudentProjectPageReqVO pageVO) {
        PraxisProjectPageReqDTO pageReqDTO=new PraxisProjectPageReqDTO ();
        SortingField sortField = pageVO.getSortField();
        if(null==sortField){
            sortField=new SortingField();
            sortField.setOrder("desc");
            sortField.setField("t.weight");
            pageVO.setSortField(sortField);
        }
        BeanUtils.copyProperties(pageVO,pageReqDTO);
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(null!=loginUser){
            pageReqDTO.setStudentId(loginUser.getId());
        }
        PageResult<PraxisProjectRespDTO> page = praxisProjectApi.getPraxisProjectPage(pageReqDTO);
        List<PraxisProjectRespDTO> list = page.getList();
        PageResult<StudentProjectVO> pageResult=new PageResult<>();
        if(CollectionUtils.isNotEmpty(list)){
            pageResult.setTotal(page.getTotal());
            List<StudentProjectVO> rList=new ArrayList<>();
            list.stream().forEach(m->{
                StudentProjectVO r=new StudentProjectVO();
                BeanUtils.copyProperties(m,r);
                rList.add(r);
            });
            pageResult.setList(rList);
        }
        return success(pageResult);
    }


    @GetMapping("/get")
    @ApiOperation("(学生端)获得实践项目")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true,   dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<StudentProjectVO> getPraxisProject(@RequestParam("id") Long id) {
        PraxisProjectRespDTO project = praxisProjectApi.getPraxisProjectById(id);
        StudentProjectVO rProject=new StudentProjectVO();
        if(null!=project){
            BeanUtils.copyProperties(project,rProject);
            List<PraxisAnnexBaseDTO> annexList = praxisProjectApi.getPraxisAnnexList(id, 1);
            List<StudentAnnexVO> aList=new ArrayList<>();
            if(CollectionUtils.isNotEmpty(annexList)){
                annexList.stream().forEach(m->{
                    StudentAnnexVO b=new StudentAnnexVO();
                    BeanUtils.copyProperties(m,b);
                    aList.add(b);
                });
                rProject.setAnnexVOList(aList);
            }
            List<PraxisProjectScheduleDTO> pList = praxisProjectApi.getPraxisProjectScheduleList(id);
            List<StudentProjectScheduleVO> sList=new ArrayList<>();
            if(CollectionUtils.isNotEmpty(pList)){
                PraxisProjectScheduleDTO scheduleDTO = pList.stream().filter(m -> (1 == m.getScheduleType())).findFirst().orElse(null);
                rProject.setStartTime(scheduleDTO.getStartTime());
                rProject.setEndTime(scheduleDTO.getEndTime());
                rProject.setClosingDate(scheduleDTO.getClosingDate());
                pList.stream().forEach(m->{
                    StudentProjectScheduleVO b=new StudentProjectScheduleVO();
                    BeanUtils.copyProperties(m,b);
                    sList.add(b);
                });
                rProject.setScheduleVOList(sList);
            }
        }
        return success(rProject);
    }

    @PostMapping("/getProjectResultPage")
    @ApiOperation("(学生端)获得项目结果分页")
    @TenantIgnore
    public CommonResult<PageResult<StudentProjectResultBaseVO>> getProjectResultPage(@Valid @RequestBody StudentProjectPageReqVO pageVO) {
        PraxisProjectPageReqDTO pageReqDTO=new PraxisProjectPageReqDTO ();
        BeanUtils.copyProperties(pageVO,pageReqDTO);
        pageReqDTO.setStatus(2);
        PageResult<PraxisProjectResultDTO> page = praxisProjectApi.getPraxisProjectResultPage(pageReqDTO);
        List<PraxisProjectResultDTO> list = page.getList();
        PageResult<StudentProjectResultBaseVO> pageResult=new PageResult<>();
        if(CollectionUtils.isNotEmpty(list)){
            pageResult.setTotal(page.getTotal());
            List<StudentProjectResultBaseVO> rList=new ArrayList<>();
            list.stream().forEach(m->{
                StudentProjectResultBaseVO r=new StudentProjectResultBaseVO();
                BeanUtils.copyProperties(m,r);
                rList.add(r);
            });
            pageResult.setList(rList);
        }
        return success(pageResult);
    }



    @GetMapping("/getProjectResultById")
    @ApiOperation("(学生端)获得结果详情")
    @ApiImplicitParam(name = "id", value = "项目结果ID", required = true,   dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<StudentProjectResultBaseVO> getProjectResultById(@RequestParam("id") Long id) {
        PraxisProjectResultDTO result = praxisProjectApi.getProjectResultById(id);
        StudentProjectResultBaseVO rProject=new StudentProjectResultBaseVO();
        if(null!=result){
            BeanUtils.copyProperties(result,rProject);
            List<PraxisAnnexBaseDTO> annexList =result.getAnnexList();
            List<StudentAnnexVO> aList=new ArrayList<>();
            if(CollectionUtils.isNotEmpty(annexList)){
                annexList.stream().forEach(m->{
                    StudentAnnexVO b=new StudentAnnexVO();
                    BeanUtils.copyProperties(m,b);
                    aList.add(b);
                });
                rProject.setAnnexList(aList);
            }
        }
        return success(rProject);
    }

}
